package Slides
{
	import SplineComponent.SplineComponentModel;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.xml.XMLNode;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	
	public class SlideModel extends SplineComponentModel
	{
		private var myView : SlideView;
		private var myController : SlideController;
		
		private var textArray : Array;
		private var imageArray : Array;
		private var openImageArray : Array;
		
		private var No_Of_Slides : int;
		
		private var lessonModel_object:Object=new Object;
		private var lessonView_object:Object=new Object;
		private var lessonEditMode_bool:Boolean;
		
		public function SlideModel(lessonModel:Object, lessonView:Object,lessonPath:String, lessonEditMode:Boolean)
		{
			super();
			this.lessonModel_object = lessonModel;
			this.lessonView_object = lessonView;
			this.lessonEditMode_bool=lessonEditMode;
			this.myController = new SlideController(this);
			this.myView= new SlideView(this,this.myController,this.lessonView_object,this.lessonEditMode_bool);
			this.addObserver(this.myView);
			initialize();
		}
		
		private function initialize():void{
			this.textArray=new Array;
			this.imageArray=new Array;
			this.openImageArray=new Array;
			
			this.No_Of_Slides=new int;
			this.No_Of_Slides=1;
		}
		
		public function fromViewtoModel(info : Object):void{
			switch(info.message.toString()){
				case "Add slide" :
					this.No_Of_Slides++;
					trace("I m in Add slide"+this.No_Of_Slides);
					break ;
				
				case "Remove slide" :
					this.No_Of_Slides--;
					trace("I m in Remove Slide"+this.No_Of_Slides);
					break;
					
				case "Add on slide" :
					AddImagetoSlide(info);
					trace("I m in the add on slide");
					break;
					
				case "Remove from slide" :
					RemoveImageFromSlide(info);
					trace("I m in the remove from slide");
					break;	
				
				case "Save slide" :
					SaveSlide(info);
					trace("I m in save slide");
					break;
					
				case "Open slide" :
					OpenSlide(info);
					trace("I m in open slide");
					break;	
			}
		}
		
		private function AddImagetoSlide(imageObj:Object):void{
			var img :Image=new Image;
			var imgArrayObj:Object=new Object;
			
			img.source=imageObj.source;
			img.x=0;
			img.y=0;
			img.percentHeight=100;
			img.percentWidth=100;
			imageObj.addThis.addChild(img);
			
			imageObj.addThis.x=imageObj.Xpos
			imageObj.addThis.y=imageObj.Ypos
			imageObj.addThis.percentHeight=imageObj.PercentHt
			imageObj.addThis.percentWidth=imageObj.PercentWd
			
			imgArrayObj.image=img;
			imgArrayObj.x=imageObj.addThis.x;
			imgArrayObj.y=imageObj.addThis.y;
			imgArrayObj.slideNo=imageObj.slideNo;
			imageObj.addOn.addChild(imageObj.addThis);
			imgArrayObj.childIndex=imageObj.addOn.getChildIndex(imageObj.addThis);
			this.imageArray.push(imgArrayObj);
			
		}
		
		private function RemoveImageFromSlide(imageObj : Object):void{
			
			var i : int=new int;
			i=0;
			imageObj.RemoveFrom.removeChildAt(imageObj.toremoveChildno);

			
			for each (var obj:Object in this.imageArray){
				if(obj.slideNo==imageObj.slideNo && obj.childIndex==imageObj.toremoveChildno)
				{
					trace(obj.image.source);
					this.imageArray.splice(i,1);
					break;
				}
				i++;
			}
			
		}
		
		private function SaveSlide(saveObject : Object):void{
			var newFile:File = saveObject.targetFile as File;
		    var stream:FileStream = new FileStream();
		    var XmlData : XML=new XML;
		    var Xmlnode : XMLNode=new XMLNode(1,"content");
		    
		    var tempArray:Array=new Array;
		    var cnvs : Canvas=new Canvas;
		    tempArray=saveObject.toSaveArray;
		    
		    XmlData=<SplineSlide>
		    		</SplineSlide>;
		   
		    for each(var childObject : Object in this.imageArray){
		    	
		    	cnvs=tempArray[childObject.slideNo].getChildAt(childObject.childIndex);
		    	Xmlnode.attributes.source=childObject.image.source.toString();
		    	Xmlnode.attributes.x=cnvs.x;
		    	Xmlnode.attributes.y=cnvs.y;
		    	Xmlnode.attributes.height=cnvs.height;
		    	Xmlnode.attributes.width=cnvs.width;
		    	Xmlnode.attributes.width=childObject.image.width;
		    	Xmlnode.attributes.height=childObject.image.height;
		    	Xmlnode.attributes.slideNo=childObject.slideNo;
		    	Xmlnode.attributes.childIndex=childObject.childIndex;
		    	XmlData.appendChild(Xmlnode);
		    }
		    
		    XmlData.@NoOfSlides=this.No_Of_Slides;
		    stream.open(newFile, FileMode.WRITE);
		    stream.writeUTFBytes(XmlData.toXMLString());
		    stream.close();
    	

		}
		
		private function OpenSlide(openInfoobject:Object):void{
			var loader:URLLoader=new URLLoader;
			var request:URLRequest=new URLRequest(openInfoobject.targetURL);
			loader.load(request);
			loader.addEventListener(Event.COMPLETE,openComplete);
		}
		
		private function openComplete(event:Event):void
		{
			var XmlData : XML=new XML(event.target.data);
			trace("i m in open complete lo "+XmlData.@NoOfSlides);
			this.No_Of_Slides=0;
			for each (var content : XML in XmlData.content)
			{
				trace(content.@source.toString());
				var imageArrayObject :Object=new Object;
				var image : Image=new Image;
				image.source=content.@source.toString();
				image.x=content.@x;
				image.y=content.@y;
				image.height=content.@height;
				image.width=content.@width;
				imageArrayObject.img=image;
				imageArrayObject.slideNo=content.@slideNo;
				imageArrayObject.childIndex=content.@childIndex;
				this.openImageArray.push(imageArrayObject);
			}
			
			var openCompleteObject : Object=new Object;
			openCompleteObject.message="Open complete";
			openCompleteObject.NoOfSlides=XmlData.@NoOfSlides;
			openCompleteObject.openArray=this.openImageArray;
			this.myView.fromModeltoView(openCompleteObject);
		}
	//--------------------------------communication functions----------------------------------------//
	
		public function incomingMessageGateway(infoObj_object:Object):void
		{
			var message:String=infoObj_object.message.toString();
			
			switch(message)
			{
				case "mouseUpEvent":
					trace("VideoModel------->event:mouseup  infoObj.event :"+infoObj_object.event);
					this.setChanged();
					this.notifyObservers(infoObj_object);
					break;
			}
		}
		
		private function sendInfoToLessonModel(infoObj_object:Object):void
		{
			this.lessonModel_object.incomingMessageGateway(infoObj_object);
		}
	//-----------------------------------------------------------------------------------------------//	
	}
}